package com.acme.bankapp.networking.server;

import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.client.Gender;
import com.acme.bankapp.exceptions.ClientExistsException;
import com.acme.bankapp.exceptions.InvalidMessageException;
import com.acme.bankapp.networking.SocketConnection;
import com.acme.bankapp.service.bank.BankService;

import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;

public class BankServer {
    private Bank bank;
    private final int port = 2004;

    public BankServer(Bank bank) {
        this.bank = bank;
    }

    void run() {
        String message;
        System.out.println("Waiting for connection");
        try (SocketConnection sc = new SocketConnection((new ServerSocket(port, 10)).accept());
             BankServerSkeleton bankServerSkeleton = new BankServerSkeleton(sc)) {
            bankServerSkeleton.onConnect();
            try {
                message = bankServerSkeleton.receiveMessage();
                System.out.println("client>" + message);
                if (message.equals("bye")) {
                    bankServerSkeleton.sendMessage("bye");
                } else {
                    try {
                        processMessage(message);
                        bankServerSkeleton.sendMessage("report");
                        bankServerSkeleton.sendMessage(bank);
                    } catch (InvalidMessageException | NumberFormatException e) {
                        bankServerSkeleton.sendMessage("Error");
                    } catch (ClientExistsException e) {
                        bankServerSkeleton.sendMessage("Client " + e.getMessage() + " already exists");
                    }
                }
            } catch (ClassNotFoundException classnot) {
                System.err.println("Data received in unknown format");
            }
        } catch (IOException ioException) {
            System.err.println("Socket unavailable");
        }
    }

    public void processMessage(String msg) throws InvalidMessageException, ClientExistsException, NumberFormatException {
        String[] args = msg.split(" ");
        if (args.length == 0)
            throw new InvalidMessageException();
        switch (args[0]) {
            case "add":
                if (args.length >= 5)
                    BankService.addClient(bank, args[1], args[2].equals("m") ? Gender.MALE : Gender.FEMALE,
                            Double.parseDouble(args[3]), Double.parseDouble(args[4]));
                else
                    throw new InvalidMessageException();
                break;
            default:
                throw new InvalidMessageException();
        }
    }

    public void runServer() throws IOException {
        File dumpFile = new File("in.txt");
        File serFile = new File("bank.ser");

        while (true) {
            run();
            BankService.dumpToFile(bank, dumpFile);
            BankService.saveBank(bank, serFile);
        }
    }
}

